#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1 
#define ERROR 0
#define UNDERFLOW -1
#define OVERFLOW -2
#define MAXSIZE 12500
#define MAXRC 3
typedef int Status;
typedef int ElemType;
typedef struct 
{
    int i, j;
    ElemType e;
}Triple;
typedef struct
{
    Triple data[MAXSIZE];
    int rpos[MAXRC];
    int mu, nu, tu;
}RLSMatrix;
Status MultSMatrix(RLSMatrix M, RLSMatrix N, RLSMatrix &Q)
{
    if (M.nu != N.mu)
        return ERROR;
    Q.mu = M.mu;
    Q.nu = N.nu;
    Q.tu = 0;
    if (M.tu * N.tu != 0)
    {
        for (int arow = 1; arow <= M.mu; ++arow)
        {
            int * ctemp = (int *)malloc((M.mu + 1) * sizeof(int));
            for (int col = 1; col <= M.nu; ++col)
                ctemp[col] = 0;
            Q.rpos[arow] = Q.tu + 1;
            int tp;
            if (arow < M.mu)
                tp = M.rpos[arow + 1];
            else
                tp = M.tu + 1;
            for (int p = M.rpos[arow]; p < tp; ++p)
            {
                int brow = M.data[p].j;
                int t;
                if (brow < N.mu)
                    t = N.rpos[brow + 1];
                else
                    t = N.tu + 1;
                for (int q = N.rpos[brow]; q < t; ++q)
                {
                    int ccol = N.data[q].j;
                    ctemp[ccol] += M.data[p].e * N.data[q].e;
                }
            }
            for (int ccol = 1; ccol <= Q.nu; ++ccol)
            {
                if (ctemp[ccol])
                {
                    if (++Q.tu > MAXSIZE)
                        return ERROR;
                    Q.data[Q.tu].i = arow;
                    Q.data[Q.tu].j = ccol;
                    Q.data[Q.tu].e = ctemp[ccol];
                }
            }
            free(ctemp);
            ctemp = NULL;
            
        }
    }
    return OK;
}

int main(void)
{
    return 0;
}